package com.cry.todo.domain.project;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import org.hibernate.envers.AuditJoinTable;
import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;
import org.hibernate.validator.constraints.Length;

import com.cry.app.core.hibernate.MultiEntities;
import com.cry.app.core.hibernate.MultiEntity;
import com.cry.todo.domain.User;

@Entity
@Table(name="todo_project")
@Audited
@org.hibernate.annotations.Entity(dynamicUpdate=true)
public class Project implements MultiEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	private Integer number;
	private String name;
	private String description;

	@ManyToOne
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private User createdBy;
	private Date createdAt;
	
	@ManyToOne
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private User supervisor;

	@ManyToOne
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private Status status;
	
	
	private int lft;
	private int rgt;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name = "todo_project_categories", 
			joinColumns = @JoinColumn(name = "project_id"), 
			inverseJoinColumns = @JoinColumn(name = "category_id"),
			uniqueConstraints = @UniqueConstraint(columnNames={"project_id","category_id"})
		)
	@AuditJoinTable
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private List<Category> categories;

	@ManyToMany
	@JoinTable(name = "todo_project_users", 
			joinColumns = @JoinColumn(name = "project_id"), 
			inverseJoinColumns = @JoinColumn(name = "user_id"),
			uniqueConstraints = @UniqueConstraint(columnNames={"project_id","user_id"})
		)	
	@AuditJoinTable
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private List<User> users;

	@ManyToOne
	private Project parentProject;
	
	@OneToMany
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private List<Comment> comments;
	
	@OneToOne(fetch=FetchType.EAGER)
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
	private Comment lastComment;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}

	/**
	 * We need to put here because of proxied entity 
	 * @return
	 */
	@NotNull
	@Length(min=1)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public User getSupervisor() {
		return supervisor;
	}
	public void setSupervisor(User supervisor) {
		this.supervisor = supervisor;
	}
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}

	public List<Category> getCategories() {
		return categories;
	}
	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public Project getParentProject() {
		return parentProject;
	}
	public void setParentProject(Project parentProject) {
		this.parentProject = parentProject;
	}
	public int getLft() {
		return lft;
	}
	public void setLft(int lft) {
		this.lft = lft;
	}
	public int getRgt() {
		return rgt;
	}
	public void setRgt(int rgt) {
		this.rgt = rgt;
	}
	public Date getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	public User getCreatedBy() {
		return createdBy;
	}
	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}
	
	public void setUsers(ArrayList<Integer> ids) throws Exception {
		setUsers(MultiEntities.getEntities(User.class, ids, getUsers()));
	}
	public void setCategories(ArrayList<Integer> ids) throws Exception {
		setCategories(MultiEntities.getEntities(Category.class, ids, getCategories())); 
		
	}
	public List<Comment> getComments() {
		return comments;
	}
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}
	public Comment getLastComment() {
		return lastComment;
	}
	public void setLastComment(Comment lastComment) {
		this.lastComment = lastComment;
	}
}
